<?php
session_start();
require_once('../includes/config.php');
require_once('includes/dbfunctions.php');

if (!isset($_SESSION['User'])) {
    require ("login.php");
    exit;
}

// If no author privileges, do not allow to edit anything
if ($_SESSION['Author'] == 0) {
    require ("index.php");
    exit;
}

$pageInfo = NULL;
$approvalError = FALSE;

// Load page info if page alias was given
$pageInfo = isset($_GET['page']) ? getPageInfo($_GET['page']) : NULL;

// If page has not been approved or is waiting for revision approval do not edit
if ($pageInfo !== NULL) {

    if ($pageInfo['Approve'] == 0 || trim($pageInfo['LastFilename']) != "") {
        header("Location: index.php?msg=waitingapproval");
    }
}

$pageTitle = ($pageInfo !== NULL) ? $pageInfo['Title'] : "";
$pageAlias = ($pageInfo !== NULL) ? $pageInfo['Alias'] : "";
$pageDesc = ($pageInfo !== NULL) ? $pageInfo['Desc'] : "";


$title = "Edit Page Content";
require('includes/header.php');
?>

        <!-- Yahoo Rich Text Editor code -->
        <!-- Skin CSS file -->
        <link rel="stylesheet" type="text/css" href="http://yui.yahooapis.com/2.7.0/build/menu/assets/skins/sam/menu.css" />
        <link rel="stylesheet" type="text/css" href="http://yui.yahooapis.com/2.7.0/build/button/assets/skins/sam/button.css" />
        <link rel="stylesheet" type="text/css" href="http://yui.yahooapis.com/2.7.0/build/fonts/fonts-min.css" />
        <link rel="stylesheet" type="text/css" href="http://yui.yahooapis.com/2.7.0/build/container/assets/skins/sam/container.css" />
        <link rel="stylesheet" type="text/css" href="http://yui.yahooapis.com/2.7.0/build/editor/assets/skins/sam/editor.css" />

        <!-- Yahoo Rich Text Editor Utility Dependencies -->
        <script type="text/javascript" src="http://yui.yahooapis.com/2.7.0/build/yahoo-dom-event/yahoo-dom-event.js"></script>
        <script type="text/javascript" src="http://yui.yahooapis.com/2.7.0/build/animation/animation-min.js"></script>
        <script type="text/javascript" src="http://yui.yahooapis.com/2.7.0/build/element/element-min.js"></script>
        <script type="text/javascript" src="http://yui.yahooapis.com/2.7.0/build/container/container-min.js"></script>
        <script type="text/javascript" src="http://yui.yahooapis.com/2.7.0/build/menu/menu-min.js"></script>
        <script type="text/javascript" src="http://yui.yahooapis.com/2.7.0/build/button/button-min.js"></script>
        <script type="text/javascript" src="http://yui.yahooapis.com/2.7.0/build/editor/editor-min.js"></script>

        <style type="text/css">
            .yui-skin-sam .yui-toolbar-container .yui-toolbar-editcode span.yui-toolbar-icon {
                background-image: url( ../images/html_editor.gif );
                background-position: 0 1px;
                left: 5px;
            }
            .yui-skin-sam .yui-toolbar-container .yui-button-editcode-selected span.yui-toolbar-icon {
                background-image: url( ../images/html_editor.gif );
                background-position: 0 1px;
                left: 5px;
            }
            .yui-skin-sam .yui-toolbar-container .yui-toolbar-insertbrief span.yui-toolbar-icon {
                background-image: url( ../images/brief.gif );
                background-position: 0 1px;
                left: 5px;
            }
            .yui-skin-sam .yui-toolbar-container .yui-button-insertbrief-selected span.yui-toolbar-icon {
                background-image: url( ../images/brief.gif );
                background-position: 0 1px;
                left: 5px;
            }
            .editor-hidden {
                visibility: hidden;
                top: -9999px;
                left: -9999px;
                position: absolute;
            }
            textarea {
                border: 0;
                margin: 0;
                padding: 0;
                font-family: monospace;
            }
            .yui-toolbar-group-insertitem {
                width: auto;
            }
        </style>
<?php require('includes/sidebar.php'); ?>
    <div class="yui-skin-sam">

        <h2>Edit Page</h2>

        <div id="editpageform">
            <form method="post" action="editpagescript.php" id="editform">
                <div class="inputrow">
                    <label for="pageurl">Page URL:</label>
                    <?php
                    if ($pageAlias === "") { ?>
                    <input type="text" name="pageurl" />
                    <?php } else { ?>
                    <input type="text" name="pageurl" value="<?php echo $pageAlias; ?>" readonly="readonly" />
                    <?php } ?>
                </div>
                <div class="inputrow">
                    <label for="pagetitle">Page Title:</label>
                    <input type="text" name="pagetitle" value="<?php echo $pageTitle; ?>" />
                </div>
                <div class="inputrow">
                    <label for="pagedesc">Page Description:</label>
                    <input type="text" name="pagedesc" value="<?php echo $pageDesc; ?>" />
                </div>

                <textarea id="pageeditor" name="editor" rows="20" cols="75">
<?php
// TODO: Change the content injection method - just dumping the file contents
// could be a security issue!
// e.g. What if the author put PHP code in the file...do we execute it?
if ($pageInfo !== NULL) {
    $filename = $contentRoot . $pageInfo['Filename'];
    if (file_exists($filename) && is_readable($filename)) {
        readfile($filename);
    }
}
?>
                </textarea>
                <div class="editpagebuttons">
                    <input type="button" id="cancelbtn" name="cancel" value="Cancel" />
                    <input type="submit" id="submitbtn" name="submit" value="Save Page" />
                </div>
            </form>

            <script type="text/javascript">

                (function() {
                    var Dom = YAHOO.util.Dom;

                    var editorToolbar = {
                        collapse: true,
                        titlebar: 'Edit Page',
                        draggable: false,
                        buttonType: 'advanced',
                        buttons: [
                            { group: 'parastyle', label: 'Paragraph Style',
                                buttons: [
                                    { type: 'select', label: 'Normal', value: 'heading', disabled: true,
                                        menu: [
                                            { text: 'Normal', value: 'none', checked: true },
                                            { text: 'Header 1', value: 'h1' },
                                            { text: 'Header 2', value: 'h2' },
                                            { text: 'Header 3', value: 'h3' },
                                            { text: 'Header 4', value: 'h4' },
                                            { text: 'Header 5', value: 'h5' },
                                            { text: 'Header 6', value: 'h6' }
                                        ]
                                    }
                                ]
                            },
                            { type: 'separator' },
                            { group: 'textstyle', label: 'Font Style',
                                buttons: [
                                    { type: 'push', label: 'Bold CTRL + SHIFT + B', value: 'bold' },
                                    { type: 'push', label: 'Italic CTRL + SHIFT + I', value: 'italic' },
                                    { type: 'push', label: 'Underline CTRL + SHIFT + U', value: 'underline' },
                                    { type: 'separator' },
                                    { type: 'push', label: 'Subscript', value: 'subscript', disabled: true },
                                    { type: 'push', label: 'Superscript', value: 'superscript', disabled: true },
                                    { type: 'separator' },
                                    { type: 'color', label: 'Font Color', value: 'forecolor', disabled: true },
                                    { type: 'color', label: 'Background Color', value: 'backcolor', disabled: true },
                                    { type: 'separator' },
                                    { type: 'push', label: 'Remove Formatting', value: 'removeformat', disabled: true },
                                    { type: 'push', label: 'Show/Hide Hidden Elements', value: 'hiddenelements' }
                                ]
                            },
                            { type: 'separator' },
                            { group: 'insertitem', label: 'Insert Item',
                                buttons: [
                                    { type: 'push', label: 'HTML Link CTRL + SHIFT + L', value: 'createlink', disabled: true },
                                    { type: 'push', label: 'Insert Image', value: 'insertimage' },
                                    { type: 'push', label: 'Insert News Brief', value: 'insertbrief' },
                                    { type: 'push', label: 'Edit HTML Code', value: 'editcode' }
                                ]
                            },
                            { type: 'separator' },
                            { group: 'alignment', label: 'Alignment',
                                buttons: [
                                    { type: 'push', label: 'Align Left CTRL + SHIFT + [', value: 'justifyleft' },
                                    { type: 'push', label: 'Align Center CTRL + SHIFT + |', value: 'justifycenter' },
                                    { type: 'push', label: 'Align Right CTRL + SHIFT + ]', value: 'justifyright' },
                                    { type: 'push', label: 'Justify', value: 'justifyfull' }
                                ]
                            },
                            { type: 'separator' },
                            { group: 'indentlist', label: 'Indenting&nbsp;and&nbsp;Lists',
                                buttons: [
                                    { type: 'push', label: 'Indent', value: 'indent', disabled: true },
                                    { type: 'push', label: 'Outdent', value: 'outdent', disabled: true },
                                    { type: 'push', label: 'Create an Unordered List', value: 'insertunorderedlist' },
                                    { type: 'push', label: 'Create an Ordered List', value: 'insertorderedlist' }
                                ]
                            }
                        ]
                    }

                    var myConfig = {
                        height: '400px',
                        width: '700px',
                        animate: true,
                        dompath: true,
                        focusAtStart: true,
                        css: 'body {font:62.5%/170% Arial, sans-serif; color:#565656; padding: 10px 30px 10px 15px;} ' +
                            'img {border:0;} ' +
                            'h1 {margin-bottom:6px; font-size:150%; color:#464666;} ' +
                            'h2 {margin:5px 0; font-size:130%; color:#886600;} ' +
                            'h3 {margin:2px 0; font-size:120%; color:#464666} ' +
                            'p {margin:2px 0 4px 0; font-size:120%;} ' +
                            'a {text-decoration: none; border-bottom:1px solid #ddddcc; color:#666600;} ' +
                            '.intro {font-size:130%; line-height:150%; color:#505066;} ' +
                            '.small {font-size:100%; line-height:150%; font-style:italic; color:#505066;} ' +
                            '.caption {font-size:100%; line-height:150%; font-style:italic; color:#505066;} ' +
                            'ul {margin:2px 0 4px 2px;} ' +
                            'ul li {padding-left:10px; list-style-type:none; font-size:110%; background:url(../images/bulletpurple.png) no-repeat left 7px;} ' +
                            'li a {text-decoration:none; border-bottom:1px solid #ddddcc; color:#666600;} ' +
                            'ol {margin:2px 0 4px 22px;} ' +
                            'ol li {font-size:110%;} ' +
                            'table {width:100%; margin:4px 0; text-align:left; font-size:110%; color:#666600;} ' +
                            'table a {border-bottom:1px solid #ddddcc; color:#666600;} ' +
                            '.tablecol1 {width: 50%;} ' +
                            '.tablecol2 {width: 25%;} ' +
                            '.tablecol3 {width: 25%;} ' +
                            'td, th {padding-right:10px; vertical-align:top; border-top: 1px dotted #ddddcc;} ' +
                            'thead th {font-weight:normal; color:#505066} ' +
                            'tfoot {font-size:95%;} ' +
                            'img {float:left; margin:4px 15px 5px 0;} ' +
                            'img.full {width:470px; display:block; float:none; margin:15px 0 5px 0;} ' +
                            '.imgcap {float:left; margin:4px 15px 0px 0;} ' +
                            '.imgcap img {float:none; display:block; margin:0} ' +
                            '.brief {width:100%; float:left; padding-top:10px; border-top:1px dotted #ddcc77;} ' +
                            '.brief img {float:left; margin:4px 15px 5px 0;} ' +
                            '.brief p {margin-bottom:15px;} ' +
                            '.brief h2 {margin-top:0; } ' +
                            '.brief h2 a {padding-left:15px; background:url(../images/arrowgold.png) no-repeat 0 5px; color:#886600;}',
                        toolbar: editorToolbar
                    };

                    var state = 'off';
                    YAHOO.log('Set state to off..', 'info', 'example');

                    YAHOO.log('Create the Editor..', 'info', 'example');
                    var myEditor = new YAHOO.widget.Editor('pageeditor', myConfig);
                    myEditor.on('toolbarLoaded', function() {

                        //When the toolbar is loaded, add a listener to the insertimage button
                        this.toolbar.on('insertimageClick', function() {
                            //Get the selected element
                            var _sel = this._getSelectedElement();
                            //If the selected element is an image, do the normal thing so they can manipulate the image
                            if (_sel && _sel.tagName && (_sel.tagName.toLowerCase() == 'img')) {
                                //Do the normal thing here..
                            } else {
                                //They don't have a selected image, open the image browser window
                                win = window.open('imagebrowser.php', 'IMAGE_BROWSER',
                                'width=800,height=500,toolbar=0,resizable=1,status=0,scrollbars=1');
                                if (!win) {
                                    //Catch the popup blocker
                                    alert('Please disable your pop-up blocker to show the image browser!');
                                }
                                //This is important.. Return false here to not fire the rest of the listeners
                                return false;
                            }
                        }, this, true);

                        this.toolbar.on('insertbriefClick', function() {
                            var briefHTML = '<div class="brief"><img src="../images/change_photo.gif" alt="" width="90" height="90"' +
                                '<h2><a href="#">Title Link</a></h2>' +
                                '<p>Enter brief text here...<a href="#">more</a></p></div>\n'
                            this.execCommand('inserthtml', briefHTML);
                        }, this, true);


                        this.toolbar.on('editcodeClick', function() {
                            var ta = this.get('element'),
                            iframe = this.get('iframe').get('element');

                            if (state == 'on') {
                                state = 'off';
                                this.toolbar.set('disabled', false);
                                YAHOO.log('Show the Editor', 'info', 'example');
                                YAHOO.log('Inject the HTML from the textarea into the editor', 'info', 'example');
                                this.setEditorHTML(ta.value);
                                if (!this.browser.ie) {
                                    this._setDesignMode('on');
                                }

                                Dom.removeClass(iframe, 'editor-hidden');
                                Dom.addClass(ta, 'editor-hidden');
                                this.show();
                                this._focusWindow();
                            } else {
                                state = 'on';
                                YAHOO.log('Show the Code Editor', 'info', 'example');
                                this.cleanHTML();
                                YAHOO.log('Save the Editors HTML', 'info', 'example');
                                Dom.addClass(iframe, 'editor-hidden');
                                Dom.removeClass(ta, 'editor-hidden');
                                this.toolbar.set('disabled', true);
                                this.toolbar.getButtonByValue('editcode').set('disabled', false);
                                this.toolbar.selectButton('editcode');
                                this.dompath.innerHTML = 'Editing HTML Code';
                                this.hide();
                            }
                            return false;
                        }, this, true);

                        this.on('cleanHTML', function(ev) {
                            YAHOO.log('cleanHTML callback fired..', 'info', 'example');
                            this.get('element').value = ev.html;
                        }, this, true);

                        this.on('afterRender', function() {
                            var wrapper = this.get('editor_wrapper');
                            wrapper.appendChild(this.get('element'));
                            this.setStyle('width', '100%');
                            this.setStyle('height', '100%');
                            this.setStyle('visibility', '');
                            this.setStyle('top', '');
                            this.setStyle('left', '');
                            this.setStyle('position', '');

                            this.addClass('editor-hidden');
                        }, this, true);
                    }, myEditor, true);

                    myEditor.on('afterOpenWindow', function() {
                        //When the window opens, disable the url of the image so they can't change it
                        var url = Dom.get('pageeditor_insertimage_url');
                        if (url) {
                            //url.disabled = true;
                        }
                    }, myEditor, true);

                    myEditor.render();

                })();
            </script>
        </div>

    </div>
<?php require('includes/footer.php'); ?>
